package com.wanli.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanli.common.BaseController;
import com.wanli.common.Result;
import com.wanli.domain.dto.GoodsDto;
import com.wanli.domain.entity.Goods;
import com.wanli.domain.entity.SysGoodsCategoryBusiness;
import com.wanli.service.GoodsService;
import com.wanli.service.SysGoodsCategoryBusinessService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 张雨润
 * @date 2024/11/9 20:35
 * @Description
 */
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private SysGoodsCategoryBusinessService sysGoodsCategoryBusinessService;

    /**
     * 获取餐品列表
     * @param goodsName
     * @return
     */
    @GetMapping("/list")
    public Result list(String goodsName) {
        Page<Goods> goodsList = goodsService.page(getPage(),
                new QueryWrapper<Goods>
                        ().like(StrUtil.isNotBlank(goodsName), "goods_name", goodsName));
        return Result.success(goodsList);
    }

    /**
     * 新增餐品
     * @param dto
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody GoodsDto dto) {
        return goodsService.saveGoods(dto);
    }

    /**
     * 更新餐品
     * @param dto
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody GoodsDto dto) {
        return goodsService.updateGoods(dto);
    }

    /**
     * 删除餐品
     * @param goodsIds
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody String[] goodsIds) {
        // 删除餐品列表
        goodsService.remove(new QueryWrapper<Goods>().in("goods_id", goodsIds));

        // 删除关系表
        sysGoodsCategoryBusinessService.remove(new QueryWrapper<SysGoodsCategoryBusiness>().in("goods_id", goodsIds));
        return Result.success("餐品信息删除成功");
    }

    /**
     * 获取新增时，选取的种类和商铺列表
     * @return
     */
    @GetMapping("/dict")
    public Result getDict() {
        return goodsService.getDict();
    }

    /**
     * 根据餐品goodsId，获取其种类与店铺信息
     * @param goodsId
     * @return
     */
    @GetMapping("/getDictById/{goodsId}")
    public Result getDictById(@PathVariable("goodsId") String goodsId) {
        return goodsService.getDictById(goodsId);
    }

    /**
     * 统计美食数量
     * @return
     */
    @GetMapping("/count")
    public Result getGoodsCount() {
        return Result.success(goodsService.count());
    }
}
